const app = getApp();
Page({
    data:{
        isInfo:false,
        avatar:'../../images/header.png',
        name: '',
        auth: false,
        type: '',
        state:false,
        reg:false,
        work:false,
        array:[
            {
                id:0,
                name:'闲'
            },
            {
                id: 1,
                name: '忙'
            }
        ],
    },
    onLoad(){
        this.loadInfo();
    },
    onPullDownRefresh (){
        this.loadInfo()
        wx.stopPullDownRefresh()
    },
    loadInfo(){
        app.axios('/info','GET',response=>{
            if (response.infoed){
                this.setData({
                    isInfo : true,
                    avatar : response.avatar,
                    name   : response.name,
                    auth   : !!response.auth,
                    type   : response.type,
                    state  : !!response.state,
                    reg    : !!response.reg,
                    work   : !!response.work
                })

                console.log(this.data.type)
                if (this.data.type == null) {
                    wx.redirectTo({
                        url: '/pages/work/index'
                    })
                }
            }
        })
    },
    getInfo(){
        wx.getUserInfo({
            success:res=>{
                // console.log(res)
                app.axios('/info','PUT',response=>{
                    if (response.infoed){
                        this.setData({
                            isInfo : true,
                            avatar : response.avatar,
                            name   : response.name,
                            auth   : !!response.auth,
                            type   : response.type,
                            state  : !!response.state,
                            reg    : !!response.reg,
                            work   : !!response.work
                        })
                    }
                    wx.redirectTo({
                      url: '/pages/work/index'
                    })
                },res)
            },
            fail:res=>{
                // console.log(res)
            }
        })
    },
    details(){
        wx.navigateTo({
          url: '/pages/user/index'
        })
    },
    authentication(){
        if (!this.data.auth){
            wx.navigateTo({
                url: '/pages/auth/index'
            })
        }
    },
    teamReg(){
        if (this.data.isInfo && this.data.type==1){
            wx.navigateTo({
                url: '/pages/reg/index'
            })
        } else {
            wx.navigateTo({
                url:'/pages/group/index'
            })
        }

    },
    message(){
        wx.navigateTo({
            url:'/pages/message/index'
        })
    },
    works(){
        wx.navigateTo({
            url:'/pages/works/index'
        })
    },
    browse(){
        wx.navigateTo({
            url:'/pages/hproject/index'
        })
    },
    hitem(){
        wx.navigateTo({
            url:'/pages/hitem/index'
        })
    },
    myroject(){
        wx.navigateTo({
            url:'/pages/mproject/index'
        })
    },
    edit(){
        wx.navigateTo({
            url:'/pages/edit/index'
        })
    },
    changeStated(){
        if (!this.data.reg){
            wx.showModal({
                title: '提示信息',
                content: '请先完成班组注册吧~',
                showCancel: false
            })
        }else {
            wx.showModal({
                title: '提示信息',
                content: '改变该状态来改变您当前的工作状态',
                showCancel: false,
                success:res=>{
                    app.axios('/changeState','PUT',response=>{
                        this.loadInfo()
                    })
                }
            })
        }

    },
    bindPickerChangeState(e){
        let value = e.detail.value
        app.axios('/changeState','POST',response=>{
            this.loadInfo();
        },{state:value})
    }
});
